//
//  JCWRiskAssessmentResultVC.swift
//  HengYouCai
//
//  Created by wll on 2017/12/7.
//  Copyright © 2017年 lwj. All rights reserved.
//  风险评测界面--代码

import UIKit

class JCWRiskAssessmentResultVC: BaseViewController {
    
    deinit {
        debugPrint("JCWRiskAssessmentResultVC 释放了")
    }
    
    private func clearStackSameVC() {
        var stackVCArr = self.navigationController?.viewControllers ?? []
        let totalCount: Int = (self.navigationController?.viewControllers.count ?? 0) - 1
        for i in 0..<totalCount {
            debugPrint(i)
            guard let vc = self.navigationController?.viewControllers[i] else {continue}
            if vc.isMember(of: RiskAppraiseViewC.self) || vc.isMember(of: JCWRiskAssessmentResultVC.self) {
                stackVCArr.remove(at: i)
            }
        }
        self.navigationController?.viewControllers = stackVCArr
    }
    
    ///风险评测类型
    var resultType: JCWRiskAssessmentResultType?
    ///视图样式类型
    var resultView: JCWRiskAssessmentResultView?
    
    ///OC专用 改写完可以干掉
    var typeIndex: Int = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        ///清除栈中的答题界面和结果展示界面
        self.clearStackSameVC()
        
        self.resultType = JCWRiskAssessmentResultType(rawValue: self.typeIndex)
        self.createNav()
        self.createCV()
        // 这两句代码不要写反了 否则没网情况下会隐藏不了hub 这tmd的找了一个小时
        self.view.jcw_showGifLoading()
        self.loadData()
        
    }

    private func createNav() -> Void {
        self.navigationItem.title = "风险评测"
        ///进去型不要右边导航按钮
        if self.resultType == .jqx {
            return
        }
        let barBtn = UIButton(type: .custom)
        barBtn.setTitleColor(Normal_333Color, for: .normal)
        barBtn.titleLabel?.font = Font_WL(15)
        barBtn.setTitle("一键默认", for: .normal)
        barBtn.addTarget(self, action: #selector(rightBarClick), for: .touchUpInside)
        barBtn.sizeToFit()
        let rightBar = UIBarButtonItem(customView: barBtn)
        self.navigationItem.rightBarButtonItem = rightBar
        
    }

    private func createCV() -> Void {
        self.view.backgroundColor = BgView_f3f3f3Color
        
        weak var weakSelf = self
        //加载结果视图  更新放在请求之后
        self.resultView = JCWRiskAssessmentResultView(CGRect(x: 10, y: kJcwGetNavHeight() + 10, width: self.view.frame.size.width-20, height: self.view.frame.size.height - kJcwGetNavHeight() - 20), self.resultType, .big, { (index, btnTitle) in
            if index == -1 {//完成
                weakSelf?.popTheVC()
            }else if index == -2 {//重新测评 进入答题界面
                weakSelf?.navigationController?.pushViewController(RiskAppraiseViewC(), animated: true)
            }
        })
        self.view.addSubview(self.resultView!)
    }
    
    ///MARK:导航右点击事件
    func rightBarClick() -> Void {
        self.view.jcw_showGifLoading()
        self.oneKeyDefault()
    }
}




extension JCWRiskAssessmentResultVC {
    
    ///数据请求 根据风险评测类型获取描述
    fileprivate func loadData() {
        
        weak var weakSelf = self
        HYCNetService.estimateLevel(withDict: ["levelType":self.resultType?.rawValue ?? 4], onSuccess: { (result) in
            weakSelf?.view.hideHUDLoading(0)
            guard let dic = result else {return}
            let json = JSON(dic)
            //更新结果视图
            self.resultView?.updateContentTV(json["tipsOne"].stringValue, json["tipsTwo"].stringValue,self.resultType)
            
        }) { (code, errMsg) in
            

            weakSelf?.view.showHUDFailTips(errMsg ?? "", hideDelay: kHUDHideDelay)

        }
    }
    ///一键默认
    fileprivate func oneKeyDefault(){
        HYCNetService.getUpdateUserDefaultLevel(onSuccess: { (result) in
            guard let dic = result else {
                self.view.hideHUDLoading(0)
                return
            }
            let json = JSON(dic)
            
            UserDefaults.standard.set(json["max_type"].intValue, forKey: kUserLevel)
            
            self.view.showHUDSuccessTips("测评成功", hideDelay: kHUDHideDelay)
            self.perform(#selector(self.popTheVC), with: nil, afterDelay: kHUDHideDelay)
        }) { (code, errMsg) in
            self.view.showHUDFailTips(errMsg, hideDelay: kHUDHideDelay)
        }
    }

}
